package com.xrui.hbase.filter;//package com.tr.ts.hbase.filter;
//
//import com.google.common.base.Objects;
//import com.tr.ts.ColumnName;
//import com.tr.ts.DataRequest;
//import com.tr.ts.DecodedCell;
//import com.tr.ts.IOException;
//import com.tr.ts.hbase.impl.HBaseColumnName;
//import org.apache.avro.Schema;
//import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
//import org.apache.hadoop.hbase.filter.Filter;
//import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
//import org.apache.hadoop.hbase.filter.SkipFilter;
//import org.codehaus.jackson.JsonNode;
//import org.codehaus.jackson.node.JsonNodeFactory;
//import org.codehaus.jackson.node.ObjectNode;
//
///**
// * A RowFilter that only includes rows where a specific column's most recent value
// * equals a specified value.
// *
// * <p>This filter will only pass if the data in the cell matches the specified value
// * <i>exactly</i>.  Both the data and the Avro schema must be the same.</p>
// */
//
//public final class ColumnValueEqualsRowFilter extends RowFilter {
//  /** The name of the family node. */
//  private static final String FAMILY_NODE = "family";
//
//  /** The name of the qualifier node. */
//  private static final String QUALIFIER_NODE = "qualifier";
//
//  /** The name of the value node. */
//  private static final String VALUE_NODE = "value";
//
//  /** The name of the writer schema node. */
//  private static final String SCHEMA_NODE = "writerSchema";
//
//  /** The name of the data node. */
//  private static final String DATA_NODE = "data";
//
//  /** The name of the column family to check for data in. */
//  private final String mFamily;
//
//  /** The name of the column qualifier to check for data in. */
//  private final String mQualifier;
//
//  /** The value the most recent column value must equal to pass the filter. */
//  private final DecodedCell<?> mValue;
//
//  /**
//   * Creates a new <code>ColumnValueEqualsRowFilter</code> instance.
//   *
//   * @param family The column family of interest.
//   * @param qualifier The column qualifier of interest.
//   * @param value The value the most recent cell in the column must equal to pass the filter.
//   */
//  public ColumnValueEqualsRowFilter(String family, String qualifier, DecodedCell<?> value) {
//    if (null == family || family.isEmpty()) {
//      throw new IllegalArgumentException("family is required");
//    }
//    if (null == qualifier || qualifier.isEmpty()) {
//      throw new IllegalArgumentException("qualifier is required");
//    }
//    if (null == value) {
//      throw new IllegalArgumentException(
//          "value may not be null. If you want to check for column data presence, use "
//          + HasColumnDataRowFilter.class.getFamily());
//    }
//    mFamily = family;
//    mQualifier = qualifier;
//    mValue = value;
//  }
//
//  /** {@inheritDoc} */
//  @Override
//  public DataRequest getDataRequest() {
//    return DataRequest.create(mFamily, mQualifier);
//  }
//
//  /** {@inheritDoc} */
//  @Override
//  public Filter toHBaseFilter(Context context) throws IOException {
//    // Create a filter that accepts a cell from mFamily and mQualifier only if it is
//    // equals to the specified value.
//    final ColumnName column = ColumnName.create(mFamily, mQualifier);
//    HBaseColumnName hbaseColumnName = context.getHBaseColumnName(column);
//    SingleColumnValueFilter filter = new SingleColumnValueFilter(
//        hbaseColumnName.getFamily(),
//        hbaseColumnName.getQualifier(),
//        CompareOp.EQUAL,
//        context.getHBaseCellValue(column, mValue));
//
//    filter.setLatestVersionOnly(true);
//    filter.setFilterIfMissing(true);
//
//    // Skip the entire row if the filter does not allow the column value.
//    return new SkipFilter(filter);
//  }
//
//  /** {@inheritDoc} */
//  @Override
//  public boolean equals(Object other) {
//    if (!(other instanceof ColumnValueEqualsRowFilter)) {
//      return false;
//    } else {
//      final ColumnValueEqualsRowFilter otherFilter = (ColumnValueEqualsRowFilter) other;
//      return Objects.equal(otherFilter.mFamily, this.mFamily)
//          && Objects.equal(otherFilter.mQualifier, this.mQualifier)
//          && Objects.equal(otherFilter.mValue, this.mValue);
//    }
//  }
//
//  /** {@inheritDoc} */
//  @Override
//  public int hashCode() {
//    return Objects.hashCode(mFamily, mQualifier, mValue);
//  }
//
//  /** {@inheritDoc} */
//  @Override
//  protected JsonNode toJsonNode() {
//    final ObjectNode root = JsonNodeFactory.instance.objectNode();
//    root.put(FAMILY_NODE, mFamily);
//    root.put(QUALIFIER_NODE, mQualifier);
//    final ObjectNode value = root.with(VALUE_NODE);
//    // Schema's documentation for toString says it is rendered as JSON.
//    value.put(SCHEMA_NODE, mValue.getWriterSchema().toString());
//    try {
//      value.put(DATA_NODE, ToJson.toAvroJsonString(mValue.getData(), mValue.getWriterSchema()));
//    } catch (IOException ioe) {
//      throw new IOException(ioe);
//    }
//    return root;
//  }
//
//  /** {@inheritDoc} */
//  @Override
//  protected Class<? extends RowFilterDeserializer> getDeserializerClass() {
//    return ColumnValueEqualsRowFilterDeserializer.class;
//  }
//
//  /** Deserializes {@code ColumnValueEqualsRowFilter}. */
//  public static final class ColumnValueEqualsRowFilterDeserializer
//      implements RowFilterDeserializer {
//    /** {@inheritDoc} */
//    @Override
//    public RowFilter createFromJson(JsonNode root) {
//      final String family = root.path(FAMILY_NODE).getTextValue();
//      final String qualifier = root.path(QUALIFIER_NODE).getTextValue();
//      final String schema = root.path(VALUE_NODE).path(SCHEMA_NODE).getTextValue();
//      final Schema writerSchema = (new Schema.Parser()).parse(schema);
//      final String data = root.path(VALUE_NODE).path(DATA_NODE).getTextValue();
//      try {
//        final DecodedCell<?> cell = new DecodedCell<Object>(writerSchema,
//            FromJson.fromAvroJsonString(data, writerSchema));
//        return new ColumnValueEqualsRowFilter(family, qualifier, cell);
//      } catch (IOException ioe) {
//        throw new IOException(ioe);
//      }
//    }
//  }
//}
